package org.crazyit.album.web;

import java.util.*;
import java.io.*;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.crazyit.album.exception.AlbumException;
import org.crazyit.album.web.base.BaseServlet;
import org.crazyit.album.vo.*;

@WebServlet(urlPatterns = "/getPhoto")
public class GetPhotoServlet extends BaseServlet {
    public void service(HttpServletRequest request
            , HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession(true);

        String name = (String) session.getAttribute("curUser");
        Object pageObj = session.getAttribute("curPage");

        int curPage = pageObj == null ? 1 : (Integer) pageObj;
        response.setContentType("text/javascript;charset=gbk");

        PrintWriter out = response.getWriter();
        try {
            List<PhotoHolder> photos = as.getPhotoByUser(name, curPage);
            out.println("var list = $('#list').empty();");
            for (PhotoHolder ph : photos) {
                out.println("list.append(\"<div align='center'>" +
                        "<a href='javascript:void(0)' onclick=\\\"showImg('"
                        + ph.getFileName() + "');\\\">"
                        + ph.getTitle() + "</a></div>\");");
            }
        } catch (AlbumException ex) {
            out.println("alert('" + ex.getMessage() + "')");
        }
    }
}
